home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #2
/
Monster Media No. 2 (Monster Media)(1994).ISO
/
prog_gen
/
psp_io.zip
/
NUMVERT.ASM
< prev
next >
Wrap
Assembly Source File
|
1994-05-28
|
12KB
|
253 lines
;****************************************************************************
; N U M V E R T . A S M
;============================================================================
; Reads number input by the user and outputs it in decimal, hexadecimal and
; binary formats.
;---------------------------------------------------------------------------
; Compile with Borland Turbo Assembler v3.0
; Link with Borland Turbo Linker v5.1
; and the files psp.obj, charstr.obj, dosio.obj & mathio.obj.
;---------------------------------------------------------------------------
; Copyright (c) Simon Groes, 1994.
;****************************************************************************
IDEAL
DOSSEG
MODEL small
STACK 256
LOCALS ; Allow local labels.
;----- INCLUDE FILES:
INCLUDE "common.inc"
INCLUDE "macro.inc"
;----- EQUATES:
;----- CL stands for 'command line'.
CL_NUM = 00000001b ; Indicates number supplied in
; command line.
CL_DEC = 00000010b ; Indicates to convert to decimal.
CL_HEX = 00000100b ; Indicates to convert to hex.
CL_BIN = 00001000b ; Indicates to convert to binary.
CL_YES = 11110000b ; Indicates command-line parameters.
DATASEG
exit_code db 0
welcome db CR, LF, 'Welcome to the Number Converter'
db CR, LF, 'Copyright (c) by Simon Groes, 1994',NULL
hinWeis db CR, LF, CR, LF
db '** INSTRUCTIONS **',CR, LF
db '------------------',CR, LF
db 'You may type in a decimal, hexadecimal or '
db 'binary number.',CR, LF
db 'If you type a hexadecimal number, '
db 'type an ''H'' or ''h'' at the end',CR,LF
db '(no spaces in between).',CR,LF
db 'A hex number must be <= 4 characters long.',CR,LF
db 'If you type a binary number, type a ''B'' or'
db ' ''b'' at the end.',CR,LF
db 'A binary number must be <= 16 characters long.'
db CR,LF
db 'Do not type any letter at the end of a '
db 'decimal number.',CR,LF
db 'A decimal number must be an integer between '
db '0 & 65535.',CR,LF
db NULL
inStrPrompt db CR,LF,'Enter a number: ',NULL
outStrHex db CR,LF,'The number in hexadecimal is: ',NULL
outStrBin db CR,LF,'The number in binary is: ',NULL
outStrDec db CR,LF,'The number in decimal is: ',NULL
errorStr db CR,LF,'Invalid number.',NULL
retryStr db CR,LF,'Try another (Y/N) ? ', NULL
inValChoice db CR,LF,'Invalid choice.',NULL
paramErrStr db 'Invalid parameter(s).', CR,LF,CR,LF,NULL
strBuf db 18 DUP (0)
strBufLen = $ - strBuf
CODESEG
;----- Declare all EXTRN procedures here:
;----- From PSP.ASM:
EXTRN PSP_Parameters:PROC, GetParamNumber:PROC, GetSwNumber:PROC
EXTRN GetNextParam:PROC
;----- From CHARSTR.ASM:
EXTRN charToUpper:proc, strCopy:PROC
;----- From DOSIO.ASM:
EXTRN dosPutString:proc, dosGetString:proc, dosGetEchoChar:proc
;----- From MATHIO.ASM:
EXTRN AsciiToBin:proc, BinToAscii:proc
Start:
call NumvertParams ; Get command-line parameters in dl.
; di -> strBuf, si -> cmdBuf
or dh, dh ; No parameters or error (dh = 0) ?
jz Default ; Yes: Continue default operation.
test dl, CL_NUM ; No : Number on command-line ?
jz Prompt ; No : Prompt for user input.
jmp NEAR Convert ; Yes: Convert number to binary.
Default:
mov di, OFFSET welcome ; Welcome user to program.
call dosPutString
mov di, OFFSET hinWeis ; Display instructions.
call dosPutString
Prompt:
mov di, OFFSET inStrPrompt ; Prompt user to enter number.
call dosPutString
mov di, OFFSET strBuf ; Dest of user input.
mov cl, strBufLen ; Max # chars + <return>.
call dosGetString ; Get user input.
xor cx, cx ; cx = 0.
; Note: Numbers entered in hex format must end with 'H' or 'h'.
; Numbers entered in binary format end with 'B' or 'b'.
Convert:
call AsciiToBin ; Convert to 16-bit integer in ax.
jc InputError ; If invalid number, error.
ConvertDec:
test dl, CL_DEC ; Convert to decimal?
jz ConvertHex ; Try hexadecimal.
;----- Output number in decimal:
mov di, OFFSET outStrDec ; 'The number in decimal is:'
call dosPutString
mov di, OFFSET strBuf ; di -> Buffer to hold number.
mov bx, 10 ; bx = base.
call BinToAscii ; Convert to ascii string.
call dosPutString ; Display number.
ConvertHex:
test dl, CL_HEX ; Convert to hexadecimal ?
jz ConvertBin ; No : Try binary.
;----- Output number in hexadecimal:
mov di, OFFSET outStrHex ; Output number in hexadecimal.
call dosPutString
mov di, OFFSET strBuf ; di -> Buffer to hold number.
mov bx, 16 ; bx = base.
call BinToAscii ; Convert to ascii string.
call dosPutString ; Display number.
ConvertBin:
test dl, CL_BIN ; Convert to binary ?
jz Retry ; No : Continue program.
;----- Output number in binary:
mov di, OFFSET outStrBin ; Output number in binary.
call dosPutString
mov di, OFFSET strBuf ; di -> Buffer to hold number.
mov bx, 2 ; bx = base.
call BinToAscii ; Convert to ascii string.
call dosPutString ; Display number.
jmp NEAR Retry ; End program.
InputError:
mov di, OFFSET errorStr ; 'Invalid number.'
call dosPutString
mov [exit_code], 1 ; End program with error.
Retry:
mov di, OFFSET retryStr ; Try another (Y/N) ?
call dosPutString
call dosGetEchoChar ; Get user's answer in al reg.
call charToUpper ; Convert to uppercase.
cmp al, 'Y' ; Yes ?
je Prompt ; Get user input.
cmp al, 'N' ; No ?
je Return ; End program.
mov di, OFFSET inValChoice ; Else, 'Invalid choice.'
call dosPutString
jmp NEAR Retry ; Try again.
Return:
mov ah, 04Ch ; DOS function: Exit program
mov al, [exit_code] ; Return exit-code value
int 21h ; Call DOS. Terminate program
;===============================================================
; Procedure: =*=NumvertParams=*=
;---------------------------------------------------------------
; Access: Private - Available only to this file.
; Task: Interpret command-line parameters of numvert program.
; Input: none
; Output: dl = flags indicating command-line parameters.
; dh = -1 (FFh) if parameters, dh = 0 if no parameters.
; di = si -> strBuf
; Registers: none
;===============================================================
PROC NumvertParams
call PSP_Parameters ; Command-line parameters
; copied to predefined buffer,
; ds = es -> dataseg,
; di = si -> param buffer.
jc @@Error ; If error in reading command line
; in PSP, end with error.
xor dx, dx ; Clear dx for flags.
call GetParamNumber ; ax = # parameters.
or ax, ax ; Any parameters ?
jz @@NoParams ; No : Terminate program.
mov cx, ax ; Yes: # parameters in cx.
xor ax, ax ; Clear ax for later use.
mov si, OFFSET strBuf ; Store offset of strBuf in si.
;---------------------------------------------------------------
; Note: Parameter # can consist of 1 positive integer, and up to
; 3 conversion choices - /b(binary), /h(hexadecimal), /d(decimal).
; The source number must be appended with the appropriate letter,
; indicating its type. If no parameters are entered it is assumed
; the user wishes to convert to dec, hex & bin. If no number is
; entered the user will be prompted in the main program.
;---------------------------------------------------------------
;----- Enter loop here:
ASSUME ds:@data, es:@data
@@R1: cmp [BYTE di], SW1 ; Is param a /switch ?
je @@SW
cmp [BYTE di], SW2 ; Is param a -switch ?
je @@SW
; Must be a number to convert.
test dl, CL_NUM ; Only 1 number can be entered.
jnz @@Error ; If already number entered, error.
xchg di, si ; Else, swap di & si.
call strCopy ; Number copied into buffer.
xchg di, si ; Restore di & si registers.
or dl, CL_NUM ; Set 'number' flag.
jmp NEAR @@LR1 ; Loop back to @@R1.
@@SW: mov al, [BYTE es:di + 1] ; Move letter into al.
call charToUpper ; Convert to upper case.
cmp al, 'D' ; Is it decimal ?
jne @@H ; No : try hex.
or dl, CL_DEC ; Yes: set decimal flag.
jmp NEAR @@LR1 ; Jump to end of loop.
@@H: cmp al, 'H' ; Is it hexadecimal ?
jne @@B ; No : try binary.
or dl, CL_HEX ; Yes: set hexadecimal flag.
jmp NEAR @@LR1 ; Jump to end of loop.
@@B: cmp al, 'B' ; Is it binary ?
jne @@Error ; No : must be error.
or dl, CL_BIN ; Yes: set binary flag.
@@LR1: call GetNextParam ; di -> next parameter in cmdBuf.
jc @@Error ; Unable to get next parameter.
loop @@R1 ; dec cx; or cx,cx; jne @@R1.
;----- Successful completion of the loop - cx = 0.
or dh, CL_YES ; Command-line parameters exist.
test dl, CL_DEC OR CL_HEX OR CL_BIN ; Any switch flags set ?
jnz @@Return ; Yes: End program.
or dl, CL_DEC OR CL_HEX OR CL_BIN ; No : set all switches.
@@Return:
mov di, si ; di -> number in strBuf.
ret ; Return to caller.
@@Error:
mov di, OFFSET paramErrStr ; 'Invalid parameter(s).'
call dosPutString
clc ; Clear carry flag.
@@NoParams:
or dl, CL_DEC OR CL_HEX OR CL_BIN ; Set all switches.
jmp NEAR @@Return ; End program.
ENDP NumvertParams
END Start ; End program / entry point.